package com.huluobo.h3kotlinproject.detail

import android.content.Context
import android.content.Intent
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.huluobo.h3kotlinproject.MainActivity
import com.huluobo.h3kotlinproject.R
import com.huluobo.h3kotlinproject.base.BaseMVVMActivity
import com.huluobo.h3kotlinproject.databinding.ActivityDetailBinding
import com.huluobo.h3kotlinproject.net.Const
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class DetailActivity : BaseMVVMActivity<ActivityDetailBinding>() {
    lateinit var detailViewModel: DetailViewModel
    private var goodsId: Int = 0

    override fun initData() {
        goodsId = intent.getIntExtra(Const.PARAM_GOODS_ID, 0)
        detailViewModel = ViewModelProvider(this)[DetailViewModel::class.java]

        if (goodsId != 0) {
            detailViewModel.getGoodsDetail(goodsId)
        }


        detailViewModel.detailSuccess.observe(this) {
            dataBinding.detailBanner.setImages(it.data.bannerList)
            dataBinding.detailBanner.start()

            dataBinding.detailDesc.text = it.data.goods_desc

            dataBinding.detailPrice.text = it.data.goods_default_price.toString()

            com.huluobo.h3kotlinproject.util.ImageLoader.loadImage(
                this,
                it.data.goods_detail_one,
                dataBinding.detailDetail1
            )

            com.huluobo.h3kotlinproject.util.ImageLoader.loadImage(
                this,
                it.data.goods_detail_two,
                dataBinding.detailDetail2
            )

            detailViewModel.addToCartSuccess.observe(this) {
                ToastUtils.showLong(it.message)
            }
        }

    }

    override fun initView() {
        //初始化banner
        //设置banner风格
        dataBinding.detailBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
        //设置banner的图片读取器
        dataBinding.detailBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                if (context != null && imageView != null) {
                    Glide.with(context).load(path).into(imageView)
                }
            }
        })
        //设置翻页时间
        dataBinding.detailBanner.setDelayTime(1500)
        //设置指示器位置
        dataBinding.detailBanner.setIndicatorGravity(BannerConfig.CENTER)

        //设置添加购物车的点击事件
        dataBinding.detailAddToCart.setOnClickListener {
            detailViewModel.addToCart(1, goodsId)
        }

        dataBinding.detailBuy.setOnClickListener {
//            val intent = Intent(this,MainActivity::class.java)
//            startActivity(intent)
//            //点击立刻购买跳转到购物车
//            Navigation.findNavController(dataBinding.root)
//                .navigate(R.id.action_nav_main_category_to_nav_main_cart)//跳转fragment,相当于隐藏和显示
        }

    }

    override fun getLayoutId(): Int = R.layout.activity_detail

}